//
//  FileManager.swift
//  SwiftDeveloper
//
//  Created by 李爽 on 15/11/13.
//  Copyright (c) 2015年 Shawn Li. All rights reserved.
//  文件操作与管理

import UIKit
import Foundation

var error: NSError?
let fileManager = NSFileManager.defaultManager()
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDir = dirPaths[0] as! String
let filelist = fileManager.contentsOfDirectoryAtPath(documentsDir, error: &error);


class FileManager {
    //获取根目录
    func getDirectory() ->String{
        let directoryString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
        return directoryString
    }
    
    //根目录下某文件
    func fileInDocumentDirectory (getFileName: String) ->String{
        return getDirectory().stringByAppendingPathComponent(getFileName)
    }
    
    
    func dirContents() -> Array<String> {
        var list: Array<String> = []
        for filename in filelist! {
            list.append("\(filename)")
        }
        return list
    }
    
    //创建文件
    func makeDir(name: String) -> Bool {
        let newDir = documentsDir.stringByAppendingPathComponent(name)
        if !fileManager.createDirectoryAtPath(newDir, withIntermediateDirectories: true, attributes: nil, error: &error) {
            return false
        } else {
            return true
        }
    }
    
    //删除文件
    func delDir(name: String) -> Bool {
        let newDir = documentsDir.stringByAppendingPathComponent(name)
        if !fileManager.removeItemAtPath(newDir, error: &error) {
            return false
        } else {
            return true
        }
    }

}